package com.example.administrator.reader.element;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.administrator.reader.R;
import com.example.administrator.reader.entity.Grid;
import com.example.administrator.reader.utils.UIUtils;
import com.example.administrator.reader.view.SGridView;

import java.util.ArrayList;
import java.util.List;


public class CGridElement extends BaseElement<List<Grid>> {
    SGridView gv;

    public CGridElement(Activity activity){
        this.activity = activity;
    }

    public GridView getGridView() {
        return gv;
    }

    @Override
    public void initView() {
        view = View.inflate(UIUtils.getContext(), R.layout.e_grid, null);
        gv = (SGridView) view.findViewById(R.id.gv);
    }

    @Override
    public void fillView() {
        gv.setAdapter(new GridAdapter());
    }

    @Override
    public void loadData(List<Grid> data1) {
        data = new ArrayList<>();
        data.add(new Grid(R.drawable.g2, "热门"));        // 大家都在看
        data.add(new Grid(R.drawable.g3, "精品"));        // 世界文学
        data.add(new Grid(R.drawable.g4, "推荐"));        // 责编推荐
        data.add(new Grid(R.drawable.g1, "免费"));        // 显示所有书籍
    }

    class GridAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder = null;
            if (null == convertView) {
                convertView = View.inflate(activity, R.layout.item_icon, null);
                viewHolder = new ViewHolder();
                viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv_book_cover);
                viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            Grid grid = data.get(position);
            viewHolder.iv.setImageResource(grid.resource);
            viewHolder.tv.setText(grid.name);
            return convertView;
        }
    }

   public class ViewHolder {
        private ImageView iv;
        private TextView tv;
    }
}

